/*
 * @Author: your name
 * @Date: 2022-02-22 22:13:20
 * @LastEditTime: 2023-04-26 12:13:34
 * @LastEditors: cool panda
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /vue3.0-ts-admin/src/utils/request.ts
 */
//1.引入vue
// import Vue from 'vue'
//2.引入axios库
import axiosNew from 'axios'
axiosNew.defaults.timeout = 60000 //请求超时时间
axiosNew.defaults.baseURL = process.env.VUE_APP_API_URL // 关键步骤–填写后台请求统一的地址
axiosNew.interceptors.request.use(
  //请求拦截
  (config) => {
    // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    // const tokenInfo = true//jsCookie.get('token')
    // if (tokenInfo) {
    //   config.headers.token = '7969b9c6-16b9-4d63-b699-6a19ba63ae63'//JSON.parse(tokenInfo).token
    // }
    return config
  },

  (err) => {
    return Promise.reject(err)
  }
)

axiosNew.interceptors.response.use(
  (response) => {
    return response
  },
  (err) => {
    return Promise.reject(err)
  }
)
const axios = function (option = {}) {
  return new Promise((resolve) => {
    axiosNew(option)
      .then((res) => {
        if (res.data.code === 401) {
          console.log('登录状态失效，请重新登录')
        }
        resolve(res.data)
      })
      .catch((err) => {
        console.log(err.message.includes(401))
        if (err.message.includes(401)) {
          //如果token为空或者过期，跳到登录
          console.log('登录状态失效，请重新登录')
        }
        // 对响应错误做点什么
        if (err.message.includes('timeout')) {
          console.log('网络超时')
        } else {
          console.log('网络连接错误')
        }
        resolve(err)
      })
  })
}
export default axios
